package com.authorization.project.authorization.controller;

import java.util.List;

import com.authorization.common.utils.StringUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.authorization.framework.aspectj.lang.annotation.Log;
import com.authorization.framework.aspectj.lang.enums.BusinessType;
import com.authorization.project.authorization.domain.PackageEntity;
import com.authorization.project.authorization.service.IPackageEntityService;
import com.authorization.framework.web.controller.BaseController;
import com.authorization.framework.web.domain.AjaxResult;
import com.authorization.common.utils.poi.ExcelUtil;
import com.authorization.framework.web.page.TableDataInfo;

/**
 * 套餐Controller
 * 
 * @author 李晓雷
 * @date 2020-04-21
 */
@RestController
@RequestMapping("/authorization/package")
public class PackageController extends BaseController
{
    @Autowired
    private IPackageEntityService packageEntityService;

    /**
     * 查询套餐列表
     */
    @PreAuthorize("@ss.hasPermi('authorization:package:list')")
    @GetMapping("/list")
    public TableDataInfo list(PackageEntity packageEntity)
    {
        startPage();
        List<PackageEntity> list = packageEntityService.selectPackageEntityList(packageEntity);
        return getDataTable(list);
    }

    /**
     * 导出套餐列表
     */
    @PreAuthorize("@ss.hasPermi('authorization:package:export')")
    @Log(title = "套餐", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(PackageEntity packageEntity)
    {
        List<PackageEntity> list = packageEntityService.selectPackageEntityList(packageEntity);
        ExcelUtil<PackageEntity> util = new ExcelUtil<PackageEntity>(PackageEntity.class);
        return util.exportExcel(list, "package");
    }

    /**
     * 获取套餐详细信息
     */
    @PreAuthorize("@ss.hasPermi('authorization:package:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(packageEntityService.selectPackageEntityById(id));
    }

    /**
     * 根据产品ID获取套餐、模块集合、选中的模块
     */
    @PostMapping(value = "/packageModuleList")
    public AjaxResult get(@RequestBody PackageEntity packageEntity)
    {
        AjaxResult ajax = AjaxResult.success();
        ajax.put("packageModuleList", packageEntityService.selectProductAnModule(packageEntity.getProductId(), packageEntity.getUserid()));
        ajax.put("checkedObjectMap", packageEntityService.checkedMmoduleList(packageEntity.getProductId(), packageEntity.getUserid(), packageEntity.getHiststate(), packageEntity.getPackageId(), packageEntity.getHistoryId()));
        return ajax;
    }

    /**
     * 新增套餐
     */
    @PreAuthorize("@ss.hasPermi('authorization:package:add')")
    @Log(title = "套餐", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody PackageEntity packageEntity)
    {
        return toAjax(packageEntityService.insertPackageModule(packageEntity));
    }

    /**
     * 修改套餐
     */
    @PreAuthorize("@ss.hasPermi('authorization:package:edit')")
    @Log(title = "套餐", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody PackageEntity packageEntity)
    {
        return toAjax(packageEntityService.updatePackageModule(packageEntity));
    }

    /**
     * 删除套餐
     */
    @PreAuthorize("@ss.hasPermi('authorization:package:remove')")
    @Log(title = "套餐", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(packageEntityService.deletePackageEntityByIds(ids));
    }
}
